capture log close
clear all
set maxvar 30000
set more off
pause off
# delimit;

local crime "";
local output "";

/* 
Table B13
RD and IV estimates of criminal justice outcomes (by age at SSI entry, pre-18 criminal record and removal probability)
*/


/* ************************* */
/* **** CRIME VARIABLES **** */
/* ************************* */

use "`crime'/prwora_crime_summstats2_exactage.dta", clear ;

keep if label == "prec_rd_mean" ;

local pre_firstlt6		"f1" ;
local pre_first7to13	"f2" ;
local pre_firstgt13		"f3" ;
local pre_record		"c" ;
local pre_remlow		"x" ;
local pre_remhigh		"r" ;

foreach sample in firstlt6 first7to13 firstgt13	record remlow remhigh {;

	preserve ;

		keep	`pre_`sample''n_crime2char* `pre_`sample''n_crime2genr* 
				`pre_`sample''n_crime2ahinc* `pre_`sample''n_crime2shinc* 
				;

		xpose, clear varname ;

		rename v1 mean ;
		rename _varname variables ;
		order variables mean ;

		replace variables = subinstr(variables,"_18_38", "", .) ;
		replace variables = subinstr(variables,"`pre_`sample''n_crime2", "", .) ;
		replace variables = subinstr(variables,"`pre_`sample''n_", "", .) ;
		replace variables = "age18_yes" if variables == "`pre_`sample''age18_yes" ; 

		tempfile `sample'_mean ;
		save ``sample'_mean' ;

	restore ;

};


clear;

foreach sample in firstlt6 first7to13 firstgt13	record remlow remhigh  {;
	foreach method in rd iv {;
		use "`crime'/prworaexact_bw264_crime_poly1allcov_`method'_`sample'.dta" ;
 
 			#delim;

			gen n_crime2 = strpos(variables, "n_crime2") ;

			keep if n_crime2 == 1 | variables == "age18_yes" | variables == 		"n_age18unfav" | variables == "n_onssi1922" ;
								  
			if "`method'" == "rd" {;

				keep variables n_post sen_post N ;
			
			};
			
			else {;
			
				keep if n_crime2 == 1  ;  				
				keep variables n_age18unfav sen_age18unfav N;
			};

			replace variables = subinstr(variables,"_18_38", "", .) ;
			replace variables = subinstr(variables,"n_crime2", "", .) ;
			replace variables = subinstr(variables,"n_", "", .) ;

			#delim;

			if "`method'" == "rd" {;
			
			local crime "char genr ahinc shinc" ;

			};
			
			else {;
			
				local crime "char genr ahinc shinc" ;

			
			};
						 
			gen keep = strpos(variables, "char") ;

			foreach var in `crime' {;

				replace keep = strpos(variables, "`var'") if keep == 0;

			};

			drop if keep == 0 ;
			drop keep ;


			merge 1:1 variables using ``sample'_mean' ;
			drop _merge ;


			gen flag_int = strpos(variables, "t") ;

			gen length = length(variables) ;
			replace flag_int = 1 if variables == "shinc" | variables == "sipp" ;
			replace flag_int = 1 if flag_int != 0 & length == 5;
			replace flag_int = 0 if flag_int != 1 ;
			drop if strpos(variables,"_18_40") ;
			
			replace variables = "Any charge" if variables == "char" | variables == "chart" ;
			replace variables = "Income-generating charge" if variables == "genr" | variables == "genrt" ;  		
			replace variables = "Incarcerated (annual likelihood/days)" if variables == "ahinc" | variables == "shinc" ;

			sort variables flag_int ;

			if "`method'" == "rd" {;

				rename n_post pt_est_rd ;
				rename sen_post se_rd ;
				gen star_rd = 2 * (normal(-abs(pt_est_rd/se_rd))) ;
				gen sta_sign_rd = "" ;
				replace sta_sign_rd = "*" if star_rd <= 0.1 & star_rd > 0.05 ;
				replace sta_sign_rd = "**" if star_rd <= 0.05 & star_rd > 0.01 ;
				replace sta_sign_rd = "***" if star_rd <= 0.01 ;
				drop star_rd ;
			};
			
			else {;
				
				rename n_age18unfav pt_est_iv ;
				rename sen_age18unfav se_iv ;
				gen star_iv = 2 * (normal(-abs(pt_est_iv/se_iv))) ;
				gen sta_sign_iv = "" ;
				replace sta_sign_iv = "*" if star_iv <= 0.1 & star_iv > 0.05 ;
				replace sta_sign_iv = "**" if star_iv <= 0.05 & star_iv > 0.01 ;
				replace sta_sign_iv = "***" if star_iv <= 0.01 ;
				drop star_iv ;
			};
			
			
			
			order variables pt_est* se* sta_sign* mean N flag_int;
			
			
			tostring flag_int, replace ;
			
			replace flag_int = "_in" if flag_int == "1" ;
			replace flag_int = "_ex" if flag_int == "0" ;
			
			drop length ;
			
			reshape wide pt_est* se* sta_sign* mean N, i(variables) j(flag_int) string ;

			tostring se*, replace ;
			replace se_`method'_ex = "(" + se_`method'_ex + ")" ;
			replace se_`method'_in = "(" + se_`method'_in + ")" ;
			
			tempfile all_`method'_`sample' ;
			save `all_`method'_`sample'' ;
			

	}; 
		
		use `all_rd_`sample'', clear ;
		
		merge 1:1 variables using `all_iv_`sample'' ;
		drop _merge ;
		
		
		order variables pt_est_rd_ex sta_sign_rd_ex se_rd_ex pt_est_iv_ex sta_sign_iv_ex se_iv_ex mean_ex N_ex
						pt_est_rd_in sta_sign_rd_in se_rd_in pt_est_iv_in sta_sign_iv_in se_iv_in mean_in N_in ;
		
		
		gen order = 1 if variables == "Any charge" ;
		replace order = 2 if variables == "Income-generating charge" ;
		replace order = 3 if variables == "Incarcerated (annual likelihood/days)" ;
		
		tempfile compiled_all_`sample' ;
		save `compiled_all_`sample'' ;		
		
};


/* *********************** */
/* **** ALL SUBGROUPS **** */
/* *********************** */
   
use `compiled_all_firstlt6', clear ;
append using `compiled_all_first7to13';
append using `compiled_all_firstgt13' ;
append using `compiled_all_record' ;
append using `compiled_all_remlow' ;
append using `compiled_all_remhigh';

insobs 1, before(1) ;
replace variables = "Age at SSI entry <=6" in 1 ;
insobs 1, before(5) ;
replace variables = "Age at SSI entry from 7 to 13" in 5 ;
insobs 1, before(9) ;
replace variables = "Age at SSI entry >13" in 9 ;
insobs 1, before(13) ;
replace variables = "Pre–18 criminal record" in 13 ;
insobs 1, before(17) ;
replace variables = "Low removal probability" in 17 ;
insobs 1, before(21) ;
replace variables = "High removal probability" in 21 ;

export excel using "`output'/tabB13.xls", firstrow(variables) ;
